package com.qiwo.webshow.controller;


import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import com.qiwo.webshow.domain.GoverTypeDO;
import com.qiwo.webshow.domain.SerisesDO;
import com.qiwo.webshow.service.SerisesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qiwo.common.base.AdminBaseController;
import com.qiwo.webshow.domain.SeriesintroDO;
import com.qiwo.webshow.service.SeriesintroService;
import com.qiwo.common.utils.Result;

/**
 * <pre>
 * 系列介绍表
 *  @author chenwenchao
 *  @date 2018-10-23 14:38:38
 * </pre>
 * <small> 2018-10-23 14:38:38 | chenwenchao</small>
 */
@Controller
@RequestMapping("/webshow/seriesintro")
public class SeriesintroController extends AdminBaseController {
    @Autowired
    private SeriesintroService seriesintroService;
    @Autowired
    private SerisesService serisesService;

    @GetMapping()
    @RequiresPermissions("webshow:seriesintro:seriesintro")
    String Seriesintro(Model model) {
        List<SerisesDO> serisesDOList = serisesService.selectList(null);
        model.addAttribute("catalogs", serisesDOList);
        return "webshow/seriesintro/seriesintro";
    }


    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("webshow:seriesintro:seriesintro")
    public Result<Page<SeriesintroDO>> list(SeriesintroDO seriesintroDTO) {

        List<SerisesDO> serisesDOList = serisesService.selectList(null);
        Wrapper<SeriesintroDO> wrapper = new EntityWrapper<SeriesintroDO>().orderBy("seriesId", false).orderBy("sort",true).orderBy("id", false);
        if (seriesintroDTO.getSeriesId() != null&&seriesintroDTO.getSeriesId()!=0) {
            wrapper.eq("series_id", seriesintroDTO.getSeriesId());
        }
        Page<SeriesintroDO> page = seriesintroService.selectPage(getPage(SeriesintroDO.class), wrapper);
        for (SeriesintroDO seriesintroDO : page.getRecords()) {
            if (seriesintroDO.getSeriesId() != null) {
                Optional<SerisesDO> optionalSerisesDO = serisesDOList.stream().filter(f -> {
                    return f.getId().equals(seriesintroDO.getSeriesId());
                }).findFirst();
                if (optionalSerisesDO.isPresent()) {
                    seriesintroDO.setSeriesName(optionalSerisesDO.get().getName());
                }
            }

        }
        return Result.ok(page);
    }

    @GetMapping("/add")
    @RequiresPermissions("webshow:seriesintro:add")
    String add(Model model) {
        List<SerisesDO> serisesDOList = serisesService.selectList(null);
        model.addAttribute("catalogs", serisesDOList);
        return "webshow/seriesintro/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("webshow:seriesintro:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        SeriesintroDO seriesintro = seriesintroService.selectById(id);
        model.addAttribute("seriesintro", seriesintro);
        List<SerisesDO> serisesDOList = serisesService.selectList(null);
        model.addAttribute("catalogs", serisesDOList);
        return "webshow/seriesintro/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("webshow:seriesintro:add")
    public Result<String> save(SeriesintroDO seriesintro) {
        seriesintroService.insert(seriesintro);
        return Result.ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("webshow:seriesintro:edit")
    public Result<String> update(SeriesintroDO seriesintro) {
        boolean update = seriesintroService.updateById(seriesintro);
        return update ? Result.ok() : Result.fail();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("webshow:seriesintro:remove")
    public Result<String> remove(Long id) {
        seriesintroService.deleteById(id);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("webshow:seriesintro:batchRemove")
    public Result<String> remove(@RequestParam("ids[]") Long[] ids) {
        seriesintroService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

}
